function KeywordsService() { }

KeywordsService.prototype = {

    /**
     * キーワードテーブル作成SQL
     * @return string
     */
    createKeywordsSQL: function(){
        return 'CREATE TABLE IF NOT EXISTS keywords ('+
            'id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' + 
            'name TEXT ' + 
        ')';
    },
    
    getKeywordByBookmarkId: function(bm_id, callback){  
        var query = 'SELECT k.id, k.name FROM keywords AS k, bookmarks_keywords AS bk' +
                    'WHERE k.id = bk.keyword_id AND bk.bookmark_id = ' + bm_id;
        var db = Connection.prototype.openDb();
        db.transaction(function(tx){
//            console.log("query: " + query);
            tx.executeSql(query, [], function(tx, results){
                callback(results);                
            }, BookmarksService.prototype.errorCallBack);
        }, BookmarksService.prototype.errorCallBack,function(){});    
    },
    
    getAllKeyword: function(callback) {
      var query = 'SELECT * FROM keywords';
      var db = Connection.prototype.openDb();
        db.transaction(function(tx){
//            console.log("query: " + query);
            tx.executeSql(query, [], function(tx, results){
                callback(results);                
            }, BookmarksService.prototype.errorCallBack);
        }, BookmarksService.prototype.errorCallBack,function(){});  
    },
    
    getKeywordByGenresId: function(genreId, callback){  
        var query = 'SELECT DISTINCT k.id, k.name, bk.bookmark_id FROM bookmarks AS b, keywords AS k, bookmarks_keywords AS bk ' +
                    'WHERE b.id = bk.bookmark_id AND k.id = bk.keyword_id AND b.genre_code = ' + genreId;
        var db = Connection.prototype.openDb();
        db.transaction(function(tx){
//            console.log("query: " + query);
            tx.executeSql(query, [], function(tx, results){
                callback(results);                
            }, BookmarksService.prototype.errorCallBack);
        }, BookmarksService.prototype.errorCallBack,function(){});    
    },
    
    checkExitsKeyword: function(keyword, callback) {
        var query = 'SELECT id FROM keywords WHERE name LIKE ' + keyword;
        var db = Connection.prototype.openDb();
        db.transaction(function(tx){
            tx.executeSql(query, [], function(tx, results){
                callback(results.rows.length);
            }, BookmarksService.prototype.errorCallBack);
        }, BookmarksService.prototype.errorCallBack,function(){}); 
    },
    
    getAddKeywordSql: function() {
        return "INSERT INTO keywords(name) VALUES (?)";
    }
};